package com.xh3140.xwidget.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.annotation.Size;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.MutableLiveData;

import com.xh3140.xwidget.dialog.callback.ConfigParamsCallback;
import com.xh3140.xwidget.dialog.listener.OnClickButtonListener;
import com.xh3140.xwidget.dialog.params.LoadingParams;
import com.xh3140.xwidget.dialog.view.FooterView;
import com.xh3140.xwidget.dialog.view.HeaderView;
import com.xh3140.xwidget.dialog.view.LoadingView;
import com.xh3140.xwidget.dialog.view.RootView;

/**
 * 加载对话框
 */
@SuppressWarnings("unused")
public final class LoadingDialog extends BaseDialog<LoadingView> {
  private LoadingParams mLoadingParams;
  private OnClickButtonListener<LoadingDialog> mOnClickButtonListener;

  @NonNull
  @Override
  protected RootView<LoadingView> createRootView(@NonNull Context context) {
    final LoadingDialog dialog = this;
    return new RootView<LoadingView>(context) {
      @Override
      protected HeaderView createHeaderView(@NonNull Context context) {
        return new HeaderView(context).configView(mHeaderParams);
      }

      @Override
      protected LoadingView createBodyView(@NonNull Context context) {
        return new LoadingView(context).configView(mLoadingParams);
      }

      @Override
      protected FooterView createFooterView(@NonNull Context context) {
        return new FooterView(context).configView(dialog, mFooterParams, mOnClickButtonListener);
      }
    };
  }

  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    ViewModel.getDefault(this, ViewModel.class).configLoadingDialogParams(this);
    return super.onCreateView(inflater, container, savedInstanceState);
  }

  /**
   * 数据状态保存
   * 由于屏幕方向旋转等情况会导致构建数据丢失，所以需要保存
   */
  public static final class ViewModel extends BaseDialog.ViewModel {
    private final MutableLiveData<LoadingParams> mLoadingParamsData = new MutableLiveData<>();
    private final MutableLiveData<OnClickButtonListener<LoadingDialog>> mOnClickButtonListenerData = new MutableLiveData<>();

    private void configLoadingDialogParams(@NonNull final LoadingDialog dialog) {
      configBaseDialogParams(dialog);
      if (dialog.mLoadingParams == null) {
        dialog.mLoadingParams = mLoadingParamsData.getValue();
      } else {
        mLoadingParamsData.postValue(dialog.mLoadingParams);
      }
      if (dialog.mOnClickButtonListener == null) {
        dialog.mOnClickButtonListener = mOnClickButtonListenerData.getValue();
      } else {
        mOnClickButtonListenerData.postValue(dialog.mOnClickButtonListener);
      }
    }
  }

  /**
   * 加载对话框构建器
   * 设置输入对话框属性
   */
  public static final class Builder extends BaseDialog.Builder<LoadingDialog, Builder> {
    /**
     * 对话框加载主体视图配置属性
     */
    private final LoadingParams mLoadingParams;

    public Builder(@NonNull Context context) {
      super(context);
      mLoadingParams = new LoadingParams(context);
    }

    public Builder(@NonNull FragmentActivity activity) {
      super(activity);
      mLoadingParams = new LoadingParams(mDialogParams.context);
    }

    public Builder(@NonNull Fragment fragment) {
      super(fragment);
      mLoadingParams = new LoadingParams(mDialogParams.context);
    }

    /**
     * 构建对话框
     * 这个方法应该在所有配置结束后调用
     */
    @Override
    public LoadingDialog build() {
      final LoadingDialog dialog = new LoadingDialog();
      dialog.mDialogParams = mDialogParams;
      dialog.mHeaderParams = mHeaderParams;
      dialog.mFooterParams = mFooterParams;
      dialog.mLoadingParams = mLoadingParams;
      dialog.mOnClickButtonListener = mOnClickButtonListener;
      return dialog;
    }

    /**
     * 配置加载主体视图属性
     *
     * @param callback 配置回调
     */
    public Builder configLoadingParams(@NonNull ConfigParamsCallback<LoadingParams> callback) {
      callback.callback(mLoadingParams);
      return this;
    }


    /**
     * 设置加载视图是否是旋转样式
     *
     * @param spinner 是否是旋转样式
     */
    public Builder setLoadingSpinner(boolean spinner) {
      mLoadingParams.spinner = spinner;
      return this;
    }

    /**
     * 设置加载视图是否是无限的
     *
     * @param infinite 是否是无限的
     */
    public Builder setLoadingInfinite(boolean infinite) {
      mLoadingParams.infinite = infinite;
      return this;
    }

    /**
     * 设置旋转视图高度 px
     *
     * @param height 高度
     */
    public Builder setLoadingSpinnerHeight(@Px int height) {
      mLoadingParams.spinnerHeight = height;
      return this;
    }

    /**
     * 水平视图高度 px
     *
     * @param height 高度
     */
    public Builder setLoadingHorizontalHeight(@Px int height) {
      mLoadingParams.horizontalHeight = height;
      return this;
    }

    /**
     * 加载视图进度条背景资源ID
     *
     * @param resId 资源ID
     */
    public Builder setLoadingDrawable(@DrawableRes int resId) {
      mLoadingParams.drawable = resId;
      return this;
    }

    /**
     * 加载视图进度条最大刻度
     *
     * @param max 最大刻度
     */
    public Builder setLoadingMax(int max) {
      mLoadingParams.max = max;
      return this;
    }

    /**
     * 加载视图进度条刻度
     *
     * @param progress 刻度
     */
    public Builder setLoadingProgress(int progress) {
      mLoadingParams.progress = progress;
      return this;
    }

    /**
     * 加载视图颜色 argb
     *
     * @param color 颜色
     */
    public Builder setLoadingTintColor(@ColorInt int color) {
      mLoadingParams.tintColor = color;
      return this;
    }

    /**
     * 设置加载视图外边距
     * 格式：[start, top, end, bottom] px
     *
     * @param margins 边距
     */
    public Builder setLoadingMargins(@Nullable @Size(4) int[] margins) {
      mLoadingParams.margins = margins;
      return this;
    }

    /**
     * 设置加载视图外边距
     *
     * @param margins 边距 px
     */
    public Builder setLoadingMargins(@Px int margins) {
      return setLoadingMargins(new int[]{margins, margins, margins, margins});
    }

    /**
     * 设置加载视图外边距
     *
     * @param left   左边距 px
     * @param top    上边距 px
     * @param right  右边距 px
     * @param bottom 下边距 px
     */
    public Builder setLoadingMargins(@Px int left, @Px int top, @Px int right, @Px int bottom) {
      return setLoadingMargins(new int[]{left, top, right, bottom});
    }

    /**
     * 设置加载视图文本位置是否在上方
     *
     * @param isTop 是否在上方
     */
    public Builder setLoadingSubtitleTop(boolean isTop) {
      mLoadingParams.isTopSubtitle = isTop;
      return this;
    }

    /**
     * 设置加载视图文本
     *
     * @param text 文本
     */
    public Builder setLoadingSubtitleText(@Nullable CharSequence text) {
      mLoadingParams.text = text;
      return this;
    }

    /**
     * 设置加载视图文本位置
     *
     * @param gravity 位置
     */
    public Builder setLoadingSubtitleGravity(int gravity) {
      mLoadingParams.gravity = gravity;
      return this;
    }

    /**
     * 设置加载视图文本字体大小 sp
     *
     * @param textSize 字体大小
     */
    public Builder setLoadingSubtitleTextSize(float textSize) {
      mLoadingParams.textSize = textSize;
      return this;
    }

    /**
     * 设置加载视图文本字体颜色 argb
     *
     * @param textColor 字体颜色
     */
    public Builder setLoadingSubtitleTextColor(@ColorInt int textColor) {
      mLoadingParams.textColor = textColor;
      return this;
    }

    /**
     * 设置加载视图文本字体样式
     *
     * @param typeface 字体样式
     */
    public Builder setLoadingSubtitleTypeface(int typeface) {
      mLoadingParams.typeface = typeface;
      return this;
    }

    /**
     * 设置加载视图背景颜色 argb
     *
     * @param backgroundColor 背景颜色
     */
    public Builder setLoadingSubtitleBackgroundColor(@ColorInt int backgroundColor) {
      mLoadingParams.backgroundColor = backgroundColor;
      return this;
    }

    /**
     * 设置加载视图内边距
     * 格式：[start, top, end, bottom] px
     *
     * @param padding 边距
     */
    public Builder setLoadingSubtitlePadding(@Nullable @Size(4) int[] padding) {
      mLoadingParams.padding = padding;
      return this;
    }

    /**
     * 设置加载视图内边距
     *
     * @param padding 边距 px
     */
    public Builder setLoadingSubtitlePadding(@Px int padding) {
      return setLoadingSubtitlePadding(new int[]{padding, padding, padding, padding});
    }

    /**
     * 设置加载视图内边距
     *
     * @param left   左边距 px
     * @param top    上边距 px
     * @param right  右边距 px
     * @param bottom 下边距 px
     */
    public Builder setLoadingSubtitlePadding(@Px int left, @Px int top, @Px int right, @Px int bottom) {
      return setLoadingSubtitlePadding(new int[]{left, top, right, bottom});
    }
  }
}